<!-- 短信注册 - smsRegister  -->
<template>
  <view>
    <!-- 标题栏 -->
    <view class="head-box ss-m-b-60">
      <view class="head-title ss-m-b-20">{{$t('pages.public.setting.sign')}}</view>
      <view class="head-subtitle">{{$t('pages.public.setting.zhucecontent')}}</view>
    </view>

    <!-- 表单项 -->
    <uni-forms
      ref="smsRegisterRef"
      v-model="state.model"
      :rules="state.rules"
      validateTrigger="bind"
      labelWidth="190"
      labelAlign="left"
    >
	
	<uni-forms-item name="username" :label="$t('pages.user.info.username')">
	  <uni-easyinput
	    :placeholder="$t('pages.addressList.shuru')+$t('pages.user.info.username')"
	    v-model="state.model.username"
	    :inputBorder="false"
	  ></uni-easyinput>
	</uni-forms-item>
	
      <uni-forms-item name="mobile" :label="$t('pages.user.store.mobile')">
        <uni-easyinput
          :placeholder="$t('pages.addressList.shuru')+$t('pages.user.store.mobile')"
          v-model="state.model.mobile"
          type="number"
          :inputBorder="false"
        >
          <!-- <template v-slot:right>
            <button
              class="ss-reset-button code-btn code-btn-start"
              :disabled="state.isMobileEnd"
              :class="{ 'code-btn-end': state.isMobileEnd }"
              @tap="getSmsCode('smsRegister', state.model.mobile)"
            >
              {{ getSmsTimer('smsRegister') }}
            </button>
          </template> -->
        </uni-easyinput>
      </uni-forms-item>
	  
	  <!-- <uni-forms-item name="email" :label="$t('pages.public.setting.email')"> -->
	  <!--  <uni-easyinput
	      :placeholder="$t('pages.addressList.shuru')+$t('pages.public.setting.email')"
	      v-model="state.model.email"
	      :inputBorder="false"
	    >
			<template v-slot:right>
				<button 
					class="ss-reset-button code-btn code-btn-start"
					:disabled="state.isMobileEnd"
					:class="{ 'code-btn-end': state.isMobileEnd }"
					@tap="getEmsCode('emsRegister', state.model.email)"
				> 
					{{ getEmsTimer('emsRegister') }} 
				</button>
			</template>
		</uni-easyinput>
	  </uni-forms-item> -->
	  
	 <!-- <uni-forms-item name="code" :label="$t('pages.withdraw.verifCode')">
	    <uni-easyinput
	      :placeholder="$t('pages.withdraw.verifCode')"
	      v-model="state.model.code"
	      :inputBorder="false"
	      type="number"
	      maxlength="4"
	    ></uni-easyinput>
	  </uni-forms-item> -->
	  
	  <uni-forms-item name="invite_code" :label="$t('pages.public.setting.yaoqingma')">
	    <uni-easyinput
	      :placeholder="$t('pages.addressList.shuru')+$t('pages.public.setting.yaoqingma')"
	      v-model="state.model.invite_code"
	      :inputBorder="false"
	    ></uni-easyinput>
	  </uni-forms-item>

      <uni-forms-item name="password" :label="$t('pages.public.setting.pwd')">
        <uni-easyinput
          type="password"
          :placeholder="$t('pages.addressList.shuru')+$t('pages.public.setting.pwd')"
          v-model="state.model.password"
          :inputBorder="false"
        >
        </uni-easyinput>
      </uni-forms-item>
	  
	  <uni-forms-item name="password" :label="$t('pages.public.setting.zaicipwd')">
	    <uni-easyinput
	      type="password"
	      :placeholder="$t('pages.public.setting.zaicipwd')"
	      v-model="state.model.passwordtwo"
	      :inputBorder="false"
	    >
			<template v-slot:right>
				<button class="ss-reset-button login-btn-start" @tap="smsRegisterSubmit"> {{$t('pages.public.setting.sign')}} </button>
			</template>
	    </uni-easyinput>
	  </uni-forms-item>
    </uni-forms>
	
	
    <button class="ss-reset-button type-btn" @tap="showAuthModal('accountLogin')">
      
	  {{$t('pages.user.info.login')}}
    </button>
  </view>
</template>

<script setup>
  import { computed, ref, reactive, unref } from 'vue';
  import sheep from '@/sheep';
  import { code, account, mobile, password, email } from '@/sheep/validate/form';
  import { showAuthModal, closeAuthModal, getSmsCode, getSmsTimer, getEmsCode, getEmsTimer } from '@/sheep/hooks/useModal';
  import { useI18n } from 'vue-i18n'
  const { t }  = useI18n()
  const props = defineProps({
    agreeStatus: {
      type: Boolean,
      default: false,
    },
  });

  const smsRegisterRef = ref(null);
  
  const isLogin = computed(() => sheep.$store('user').isLogin);

  const emits = defineEmits(['onConfirm']);

  // 数据
  const state = reactive({
    isMobileEnd: false, // 手机号输入完毕
    model: {
      mobile: '', // 手机号
      // code: '', // 验证码
      password: '', // 密码
    },
    rules: {
      code,
      username: account,
      mobile,
      password,
	  email,
	  invite_code: {
		  rules: [
		    {
		      required: true,
		      errorMessage: t('pages.addressList.shuru')+t('pages.public.setting.yaoqingma'),
		    }
		  ],
	  }
    },
  });

  // 3.短信注册
  async function smsRegisterSubmit() {
	const validate = await unref(smsRegisterRef)
      .validate()
      .catch((error) => {
        console.log('error: ', error);
      });
    if (!validate) return;

    if (!props.agreeStatus) {
      emits('onConfirm',true);
      sheep.$helper.toast(t('pages.user.qgxty'));
      return;
    }
	
	if(state.model.passwordtwo !== state.model.password) {
		emits('onConfirm',true);
		sheep.$helper.toast(t('pages.public.setting.qsrxdmima'));
		return;
	}
    const { code } = await sheep.$api.user.newRegister({
      ...state.model,
      shareInfo: uni.getStorageSync('shareLog') || {},
    });
    if (code === 1) {
		const versionInfo = await sheep.$api.user.versionInfo()
		
		uni.showModal({
			content: t('popuVersion.zccg'),
			showCancel: false,
			confirmText: t('popuVersion.djxz'),
			success: (res) => {
				if (res.confirm) {
					// #ifdef H5
					location.href = versionInfo.data.downloadurl
					// #endif
					
					// #ifdef APP-PLUS
					plus.runtime.openURL(versionInfo.data.downloadurl, (err) => {
					   console.log(err);  
					})
					// #endif
				}
			},
		})
		
      closeAuthModal();
    }
  }
  
  const shareInfo = uni.getStorageSync('shareLog')
  if(shareInfo) {
	  state.model.invite_code = shareInfo.invite_code
  }
</script>

<style lang="scss" scoped>
  @import '../index.scss';
  .code-btn-start, .login-btn-start {
	  width: fit-content;
	  padding: 0 20rpx;
  }
  
  // .login-btn-start  {
	 //  margin: 0 auto;
  // }
</style>
